﻿(function ($) {

    var l = abp.localization.getResource('Edu');//EduResource的LocalizationResourceName特性

    var _appService = edu.services.studyTaskCrud;
    var _editModal = new abp.ModalManager(abp.appPath + 'StudyTask/EditModal');
    var _createModal = new abp.ModalManager(abp.appPath + 'StudyTask/CreateModal');

    $(function () {

        var _$wrapper = $('#StudyTasksWrapper');
        var _$table = _$wrapper.find('table');
        var _dataTable = _$table.DataTable(abp.libs.datatables.normalizeConfiguration({
            order: [[2, "desc"]],
			processing: true,
			serverSide: true,
			paging: true,
            ajax: abp.libs.datatables.createAjax(_appService.getList),
            columnDefs: [
                {
                    data: "title"
                },
                {
                    data: "status",
                    render: function (data) {
                        switch (data) {
                            case 1:
                                return "学习中";
                            case 2:
                                return "已完成";
                            case 3:
                                return "已过期";
                            case 4:
                                return "待审核";
                            case 5:
                                return "未批准";
                            case 6:
                                return "未打分";
                            case 7:
                                return "待复审";
                        }
                    }
                },
                {
                    data:"creationTime"
                },
                {
                    rowAction: {
                        items:
                            [
                                {
                                    text: l('Delete'),
                                    visible: abp.auth.isGranted('StudyTask.StudyTasks.Delete'),
                                    confirmMessage: function (data) { return "你确定删除" + data.record.name+"这条记录吗?"; },
                                    action: function (data) {
                                        _appService
                                            .delete(data.record.id)
                                            .then(function () {
                                                _dataTable.ajax.reload();
                                            });
                                    }
                                }
                            ]
                    }
                }
            ]
        }));

        _createModal.onResult(function () {
            _dataTable.ajax.reload();
        });

        _editModal.onResult(function () {
            _dataTable.ajax.reload();
        });

        _$wrapper.find('button[name=CreateStudyTask]').click(function (e) {
            e.preventDefault();
            _createModal.open();
        });
    });

})(jQuery);
